package com.vapps.planificaciojubilacio;

import android.app.LocalActivityManager;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;


/*
 * 
 * 
 */
public class PlanificacioJubilacio extends ScrollableTabActivity{
	private Button bMainHelp;
	private Resources res;
	
	private Intent intentDades;
	private Intent intentSituacio;
	private Intent intentPlanificacio;
	private Intent intentResultats;
	private Intent intentGrafica;
	private Intent intentPerfil;
	private Intent intentOrientacio; 
    
	//public static final String PREFS_NAME = "MyPrefsFile";
	
        @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
              
		// Gets resources instance for application's package
		res = getResources();
        
		// Attach UI widgets
        bMainHelp = (Button) findViewById(R.id.b_main_help);

		// Listeners declaration
		// //////////////////////////////////////////////////////////////////////////////
        bMainHelp.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mainHelpEnabled = !mainHelpEnabled;
				if ( mainHelpEnabled ) {
					bMainHelp.setCompoundDrawablesWithIntrinsicBounds(null, res.getDrawable(R.drawable.ic_dialog_persp_info_pres), null, null);
				}
				else {
					bMainHelp.setCompoundDrawablesWithIntrinsicBounds(null, res.getDrawable(R.drawable.ic_dialog_persp_info), null, null);
				}

				LocalActivityManager activityManager = getLocalActivityManager();
				if( activityManager.getCurrentId() == res.getString(R.string.tab_dades)) {
					intentDades.putExtra("MainHelpEnabled", mainHelpEnabled);
					activityManager.startActivity(res.getString(R.string.tab_dades), intentDades);
				}
				else if( activityManager.getCurrentId() == res.getString(R.string.tab_situacio)) {
					intentSituacio.putExtra("MainHelpEnabled", mainHelpEnabled);
					activityManager.startActivity(res.getString(R.string.tab_situacio), intentSituacio);
				}
				else if( activityManager.getCurrentId() == res.getString(R.string.tab_planificacio)) {
					intentPlanificacio.putExtra("MainHelpEnabled", mainHelpEnabled);
					activityManager.startActivity(res.getString(R.string.tab_planificacio), intentPlanificacio);
				}
				else if( activityManager.getCurrentId() == res.getString(R.string.tab_resultats)) {
					intentResultats.putExtra("MainHelpEnabled", mainHelpEnabled);
					activityManager.startActivity(res.getString(R.string.tab_resultats), intentResultats);
				}
				else if( activityManager.getCurrentId() == res.getString(R.string.tab_grafica)) {
					intentGrafica.putExtra("MainHelpEnabled", mainHelpEnabled);
					activityManager.startActivity(res.getString(R.string.tab_grafica), intentGrafica);
				}
				else if( activityManager.getCurrentId() == res.getString(R.string.tab_perfil)) {
					intentPerfil.putExtra("MainHelpEnabled", mainHelpEnabled);
					activityManager.startActivity(res.getString(R.string.tab_perfil), intentPerfil);
				}
				else if( activityManager.getCurrentId() == res.getString(R.string.tab_orientacio)) {
					intentOrientacio.putExtra("MainHelpEnabled", mainHelpEnabled);
					activityManager.startActivity(res.getString(R.string.tab_orientacio), intentOrientacio);
				}
			}
		});
        
        /*
         * set this activity as the tab bar delegate
         * so that onTabChanged is called when users tap on the bar
         */
        setDelegate(new SliderBarActivityDelegateImpl());
        
        intentDades = new Intent(this, com.vapps.planificaciojubilacio.Dades.class);
        this.addTab(res.getString(R.string.tab_dades), R.drawable.ic_menu_datos, intentDades);
        intentSituacio = new Intent(this, com.vapps.planificaciojubilacio.Situacio.class);
        this.addTab(res.getString(R.string.tab_situacio), R.drawable.ic_menu_situacion, intentSituacio);
        intentPlanificacio = new Intent(this, com.vapps.planificaciojubilacio.Planificacio.class);
        this.addTab(res.getString(R.string.tab_planificacio), R.drawable.ic_menu_planificacion, intentPlanificacio);
        intentResultats = new Intent(this, com.vapps.planificaciojubilacio.Resultats.class);
        this.addTab(res.getString(R.string.tab_resultats), R.drawable.ic_menu_resultados, intentResultats);
        intentGrafica = new Intent(this, com.vapps.planificaciojubilacio.Grafica.class);
        this.addTab(res.getString(R.string.tab_grafica), R.drawable.ic_menu_grafica, intentGrafica);
        intentPerfil = new Intent(this, com.vapps.planificaciojubilacio.Perfil.class);
        this.addTab(res.getString(R.string.tab_perfil), R.drawable.ic_menu_perfil, intentPerfil);
        intentOrientacio = new Intent(this, com.vapps.planificaciojubilacio.Orientacio.class);
        this.addTab(res.getString(R.string.tab_orientacio), R.drawable.ic_menu_orientacion, intentOrientacio);
        
        
        commit();
    }
    	
	public void onDestroy() {
    	super.onDestroy();
	}
        
    private class SliderBarActivityDelegateImpl extends SliderBarActivityDelegate
    {
        /*
         * Optional callback method
         * called when users tap on the tab bar button
         */
        protected void onTabChanged(int tabIndex) 
        {
        }
    }

	// TODO: Pendent quines opcions fiquem a menu
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.about:
			Intent intent = new Intent(this, About.class);
			startActivity(intent);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
}
